// pages/index/index.js
const app = getApp()

Page({
  data: {
    userInfo: null,
    todayRecommendation: {
      breakfast: {
        title: '早餐推荐',
        time: '07:00-08:00',
        content: '燕麦粥 + 水煮蛋 + 新鲜水果',
        tags: ['低糖', '低嘌呤', '高纤维']
      }
    }
  },

  onLoad() {
    this.checkLoginStatus()
  },

  onShow() {
    this.checkLoginStatus()
  },

  onPullDownRefresh() {
    this.checkLoginStatus()
    wx.stopPullDownRefresh()
  },

  checkLoginStatus() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        userInfo: userInfo
      })
    }
  },

  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  // 获取用户健康信息
  getUserHealthInfo() {
    const healthInfo = wx.getStorageSync('healthInfo') || {}
    if (this.data.userInfo) {
      this.setData({
        'userInfo.bloodSugar': healthInfo.bloodSugar,
        'userInfo.uricAcid': healthInfo.uricAcid,
        'userInfo.bmi': healthInfo.bmi,
        'userInfo.healthStatus': this.getHealthStatus(healthInfo)
      })
    }
  },

  // 评估健康状态
  getHealthStatus(healthInfo) {
    if (!healthInfo.bloodSugar && !healthInfo.uricAcid) {
      return '未设置'
    }
    
    let status = '良好'
    if (healthInfo.bloodSugar > 7.0 || healthInfo.uricAcid > 420) {
      status = '需关注'
    }
    if (healthInfo.bloodSugar > 11.1 || healthInfo.uricAcid > 540) {
      status = '需改善'
    }
    
    return status
  },

  // 查看推荐详情
  viewRecommendationDetail(e) {
    const type = e.currentTarget.dataset.type
    wx.showModal({
      title: this.data.todayRecommendation[type].title,
      content: this.data.todayRecommendation[type].content,
      showCancel: false
    })
  }
})